//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace datasource
{
    public partial class ConfigSources
    {
        public static Version CurrentVersion = new Version(1, 1, 0, 0);

        public Provider GetByProvider(string id)
        {
            if (this.DataSources == null)
                this.DataSources = new Provider[0];

            foreach (Provider p in this.DataSources)
            {
                if (p.name.Equals(id, StringComparison.InvariantCultureIgnoreCase))
                {
                    // Found a provider
                    return p;
                }
            }

            // Add a new provider
            Provider newProv = new Provider();
            newProv.name = id;
            newProv.DataSource = new DataSource[0];

            List<Provider> provList = new List<Provider>(this.DataSources);
            provList.Add(newProv);
            this.DataSources = provList.ToArray();

            return newProv;
        }
    }

    public partial class DataSourceProperty
    {
        public DataSourceProperty()
        {
        }

        public DataSourceProperty(string name, string value)
        {
            this.nameField = name;
            this.valueField = value;
        }
    }

    public partial class Reference
    {
        public static Reference Create(string provider, Guid id)
        {
            Reference ret = new Reference();
            ret.provider = provider;
            ret.connection_id = id.ToString();
            return ret;
        }
    }

    public partial class Provider
    {
        public DataSource GetDataSourceByID(Guid id)
        {
            if (this.DataSource == null)
                return null;

            foreach (DataSource ds in this.DataSource)
            {
                if (new Guid(ds.id).Equals(id))
                    return ds;
            }

            return null;
        }

        public void AddDataSource(datasource.DataSource item)
        {
            List<datasource.DataSource> itemList = new List<DataSource>();
            if (this.DataSource != null)
                itemList.AddRange(this.DataSource);
            itemList.Add(item);
            this.DataSource = itemList.ToArray();
        }

        public void RemoveDataSource(datasource.DataSource item)
        {
            List<datasource.DataSource> itemList = new List<DataSource>(this.DataSource);
            itemList.Remove(item);
            this.DataSource = itemList.ToArray();
        }
    }

    public partial class DataSource
    {
        public static DataSource Create(string name)
        {
            DataSource ret = new DataSource();
            ret.id = Guid.NewGuid().ToString();
            ret.name = name;
            return ret;
        }

        public string GetPropertyAsString(string field)
        {
            if (Property == null)
                return null;

            foreach (DataSourceProperty prop in Property)
            {
                if (prop.name.Equals(field, StringComparison.InvariantCultureIgnoreCase))
                    return prop.Value;
            }

            return null;
        }

        public Uri GetPropertyAsUri(string field)
        {
            string fieldData = GetPropertyAsString(field);

            if (!string.IsNullOrEmpty(fieldData))
            {
                return new Uri(fieldData);
            }

            return null;
        }

        public bool GetPropertyAsBoolean(string field)
        {
            return Convert.ToBoolean(GetPropertyAsString(field));
        }

        public Guid GetPropertyAsGuid(string field)
        {
            return new Guid(GetPropertyAsString(field));
        }

        public void SetPropertyString(string field, string value)
        {
            List<DataSourceProperty> propList = new List<DataSourceProperty>();

            if (Property == null)
                Property = new DataSourceProperty[0];

            propList.AddRange(Property);

            DataSourceProperty thisProp = null;
            foreach (DataSourceProperty prop in Property)
            {
                if (prop.name.Equals(field, StringComparison.InvariantCultureIgnoreCase))
                {
                    thisProp = prop;
                    break;
                }
            }

            if (thisProp == null)
            {
                thisProp = new DataSourceProperty(field, value);
                propList.Add(thisProp);
            }
            else
            {
                thisProp.Value = value;
            }

            Property = propList.ToArray();
        }

        public void SetPropertyBoolean(string field, bool value)
        {
            SetPropertyString(field, value.ToString());
        }

        public void SetPropertyUri(string field, Uri value)
        {
            SetPropertyString(field, value.ToString());
        }

        public void SetPropertyGuid(string field, Guid value)
        {
            SetPropertyString(field, value.ToString());
        }

        public override string ToString()
        {
            return this.name;
        }
    }
}
